package net.telesing.tcmc.back.action;

import java.io.File;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import net.telesing.tcmc.back.service.impl.ApiServiceImpl;

/**
 * 接口Action
 *
 * Created by Zmt on 2015/11/12.
 */
@At("/api")
@IocBean
public class ApiAction
{
    private static final Logger log = LogManager.getLogger(ApiAction.class);

    @Inject
    private ApiServiceImpl apiServiceImpl;

    /**
     * 下载阙音通APK
     * 
     * @param response
     * @return
     * @throws Exception
     */
    @At
    @Ok("raw")
    public File downloadApk(HttpServletResponse response) throws Exception
    {
        log.info("apiAction --> downloadApk");
        return apiServiceImpl.downloadApk(response);
    }

    /**
     * 检查版本更新
     *
     * @param code
     * @return
     */
    @At
    @Ok("json")
    public Map<String, Object> checkUpdate(@Param("code") int code)
    {
        log.info("apiAction --> checkUpdate");
        return apiServiceImpl.checkUpdate(code);
    }

    /**
     * 根据id获取号段详情
     *
     * @param id
     * @return
     */
    @At
    @Ok("json")
    public Map<String, Object> getSectionById(@Param("id") long id)
    {
        log.info("apiAction --> getSectionById");
        return apiServiceImpl.getSectionById(id);
    }

    /**
     * 根据密文获取号段详情
     *
     * @param secret
     * @return
     */
    @At
    @Ok("json")
    public Map<String, Object> getSectionBySecret(@Param("secret") String secret)
    {
        log.info("apiAction --> getSectionBySecret");
        return apiServiceImpl.getSectionBySecret(secret);
    }

    /**
     * 根据用户名、密码获取码段
     *
     * @param userName
     * @param pwd
     * @return
     */
    @At
    @Ok("json")
    public Map<String, Object> getSection(@Param("userName") String userName, @Param("pwd") String pwd)
    {
        log.info("apiAction --> getSection");
        return apiServiceImpl.getSection(userName, pwd);
    }

    /**
     * 根据用户名、密码、码段、证书类型获取证书
     *
     * @param userName
     * @param pwd
     * @param sName
     * @param cType
     * @return
     */
    @At
    @Ok("json")
    public Map<String, Object> getCertificate(@Param("userName") String userName, @Param("pwd") String pwd, @Param("sName") String sName, @Param("cType") int cType, HttpServletRequest httpServletRequest)
    {
        log.info("apiAction --> getCertificate");
        return apiServiceImpl.getCertificate(userName, pwd, sName, cType, httpServletRequest);
    }

    /**
     * 根据码段获取码段详情
     *
     * @param section
     * @return
     */
    @At
    @Ok("json")
    public Map<String, Object> getSectionByName(@Param("section") String section)
    {
        log.info("apiAction --> getSectionByName");
        return apiServiceImpl.getSectionByName(section);
    }

}
